package com.zr.util;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;

public class SendMail {
//public static String myEmailAccount="jiangyunfeng666666@163.com";
//public static String myEmailPassword="MHAFVBMEENAHRMLT";
  public static  Session session;//会话对象，用于和邮件服务器交互
    public static Properties props;
static {
//读取配置文件
    InputStream is = SendMail.class.getClassLoader().getResourceAsStream("mail.properties");
    //创建Properties对象
     props = new Properties();
    try {
        props.load(is);
    } catch (IOException e) {
        e.printStackTrace();
    }
    session=Session.getInstance(props);
    session.setDebug(true);
}
public static void sendMail(String receiveMailAccount,String messageContent,String fromPersonl,String toPersonl,String subject)throws Exception{
    MimeMessage message=new MimeMessage(session);
    message.setFrom(new InternetAddress(props.getProperty("myEmailAccount"),fromPersonl,"UTF-8"));
    message.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress(receiveMailAccount,toPersonl,"UTF-8"));
    message.setSubject(subject,"UTF-8");
    message.setContent(messageContent,"text/html;charset=UTF-8");
    message.setSentDate(new Date());
    message.saveChanges();
    Transport transport=session.getTransport();
    transport.connect(props.getProperty("myEmailAccount"),props.getProperty("myEmailPassword"));
    transport.sendMessage(message,message.getAllRecipients());
    transport.close();
}

}
